//
//  NoticeTableViewController.m
//  BiuDejie
//
//  Created by dale on 2017/3/22.
//  Copyright © 2017年 dale. All rights reserved.
//

#import "NoticeTableViewController.h"
#import "NoticeCell.h"
#import "NoticeModel.h"
#import "UserInfoModel.h"
#import "JPUSHService.h"

@interface NoticeTableViewController ()

@property (retain,nonatomic) NSMutableArray* dataArray;

@end

@implementation NoticeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知";
    // 角标清零
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [JPUSHService setBadge:0];
    
     self.dataArray = [[NSMutableArray alloc] init];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self loadData];
}

#pragma mark--加载数据
-(void)loadData{
    NSMutableDictionary* parameters=[NSMutableDictionary dictionary];
    parameters[@"sid"]=[UserInfoModel shareInstance].stu_id;
    parameters[@"token"]=[UserInfoModel shareInstance].token;
    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString:@"SDSY/getJpush" parameters:parameters success:^(NSDictionary *respondDictionary) {
        
//        // 1. 检查请求的返回值
//        if([respondDictionary[@"messages"] count] == 0){
//            [self updateFailed];
//            return ;
//        }
        // 2.储存数据
        for (NSDictionary *dic in respondDictionary[@"data"]) {
            NoticeModel* model=[[NoticeModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        //reload Data
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        [self updateFailed];
    }];
}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}




#pragma mark--UITableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* IDStr=@"id";
    NoticeCell* cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        cell=[[NoticeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDStr];
    }
    NoticeModel* model=self.dataArray[indexPath.row];
    cell.model=model;
    return cell;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[NoticeCell class] contentViewWidth:SCREEN_WIDTH];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    NoticeCell* cell=[tableView cellForRowAtIndexPath:indexPath];
    //如果type==1则为预约导师通知，否则则为活动通知
    if (cell.type==1) {
        NSLog(@"跳转导师详情页id为%d",cell.notice_id);
    }else{
        NSLog(@"跳转活动详情页id为%d",cell.notice_id);
    }

}

@end
